package com.woniuxy.hrp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniuxy.hrp.entity.Doctor;
import com.woniuxy.hrp.entity.DoctorPatientMedicine;
import com.woniuxy.hrp.entity.Reservation;
import com.woniuxy.hrp.provider.DoctorSqlProvide;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * @program: hrph
 * @InterfaceName DoctorMapper
 * @description:医生DAO
 * @author: shanshanfan
 * @create: 2022-08-29 09:38
 * @Version 1.0
 **/
@Mapper
public interface DoctorMapper extends BaseMapper<Doctor> {

    /***
     * @description: 增：医生注册
     * @param: doctor
     * @return: void
     * @author shanshanfan
     * @date: 2022/8/29 09:50
     */
    @Insert("insert into hrp_doctor(name,position_id,description,department_id,status,account,password) values(#{name},#{position.id},#{description},#{department.id},#{status},#{account},#{password})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void add(Doctor doctor);

    /*** 
     * @description: 删：根据id锁定医生帐号
     * @param: id
     * @return: void
     * @author shanshanfan
     * @date: 2022/8/29 10:20
     */
    @Update("update hrp_doctor set status='n' where id=#{id}")
    void delete(int id);

    /***
     * @description: 改：根据id认证医生帐号（默认为未认证：x)
     * @param: id
     * @return: void
     * @author shanshanfan
     * @date: 2022/8/29 10:40
     */
    @Update("update hrp_doctor set status='y' where id=#{id}")
    void authById(int id);

    /***
     * @description: 改：动态修改指定id的医生信息(姓名、帐号、状态不能修改）
     * @param: doctor
     * @return: void
     * @author shanshanfan
     * @date: 2022/8/29 10:50
     */
    @UpdateProvider(type = SqlProvider.class, method = "updateDoctor")
    void update(Doctor doctor);

    /*** 
     * @description: 查：按id查找医生
     * @param: id
     * @return: com.woniuxy.hrp.entity.Doctor
     * @author shanshanfan
     * @date: 2022/8/29 11:20
     */
    @Select("select * from hrp_doctor where id=#{id}")
    @Results(id = "doctorMap", value = {
            @Result(column = "id", property = "id", id = true),
            @Result(column = "name", property = "name"),
            @Result(column = "description", property = "description"),
            @Result(column = "status", property = "status"),
            @Result(column = "account", property = "account"),
            @Result(column = "password", property = "password"),
            //多对一，多个医生同属一个级别，懒加载
            @Result(column = "position_id", property = "position", one = @One(select = "com.woniuxy.hrp.mapper.PositionMapper.getById")),
            //多对一，多个医生同属一个部门，懒加载
            @Result(column = "department_id", property = "department", one = @One(select = "com.woniuxy.hrp.mapper.DepartmentMapper.getById"))
    })
    Doctor selectById(int id);

    /***
     * @description: 查：按科室id查找医生
     * @param: Departmentid
     * @return: com.woniuxy.hrp.entity.Doctor
     * @author WY
     */
    @Select("select * from hrp_doctor where Department_id=#{Did}")
    Doctor selectByDepartmentid(int Did);

    /***
     * @description: 查：按id查找医生排班信息
     * @param: id
     * @return: com.woniuxy.hrp.entity.Doctor
     * @author 刘磊
     * @date: 2022/8/29 11:20
     */
    @Select("select * from hrp_doctor where id=#{id}")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "name", property = "name"),
            @Result(column = "description", property = "description"),
            @Result(column = "status", property = "status"),
            @Result(column = "account", property = "account"),
            @Result(column = "password", property = "password"),
            //一对多
            @Result(column = "id", property = "arrangeTimes", many = @Many(select = "com.woniuxy.hrp.mapper.ArrangeTimeMapper.getByDoctorId", fetchType = FetchType.LAZY)),
            @Result(column = "id", property = "doctorAndTimes", many = @Many(select = "com.woniuxy.hrp.mapper.DoctorAndTimeMapper.selectbydoctorid", fetchType = FetchType.LAZY))
    })
    Doctor getPaiBanById(int id);
    /***
     * @description: 查：多条件查询医生数据（集合）
     * @param: doctor
     * @return: 符合查询条件的医生集合
     * @author shanshanfan
     * @date: 2022/8/29 11:42
     */
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "description", property = "description"),
            @Result(column = "status", property = "status"),
            @Result(column = "account", property = "account"),
            @Result(column = "password", property = "password"),
            @Result(column = "Department_id", property = "department.id"),
            @Result(column = "position_id", property = "position.id")

    })
    @SelectProvider(type = DoctorSqlProvide.class, method = "selectDoctorByCondition")
    List<Doctor> selectByCondition(Doctor doctor);

    Doctor seleAllById(@Param("id") int id);




    /***
     * @description: 用于产生动态查询、修改的sql语句
     * @author shanshanfan
     * @date: 2022/8/29 10:55
     */
    public static class SqlProvider {
        public static String updateDoctor(Doctor doctor) {
            String sql = new SQL() {
                {
                    UPDATE("hrp_doctor");
                    if (doctor.getPosition() != null ) {
                        SET("position_id=#{position.id}");
                    }
                    if (doctor.getDescription() != null ) {
                        SET("description=#{description}");
                    }
                    if (doctor.getDepartment() != null ) {
                        SET("department_id=#{department.id}");
                    }
                    if (doctor.getPassword() != null ) {
                        SET("password=#{password}");
                    }
                    WHERE("id=#{id}");
                }
            }.toString();
            return sql;
        }

    }

    /***
     * @description: 查：根据医生帐号和密码查询（登录）
     * @param: account password
     * @return: null代表账号或密码错误；doctor代表帐号密码正确
     * @author shanshanfan
     * @date: 2022/8/29 11:35
     */
    @Select("select * from hrp_doctor where account=#{account} and password=#{password}")
    Doctor selectByAccAndPsw(@Param("account") String account, @Param("password") String password);
/**
 * @Author  李国杰
 * @Description //按照账号查询
 * @Date 20:38 2022/9/5
 * @Param
 * @return
 **/
    @Select("select * from hrp_doctor where account=#{account}")
    Doctor selectByAcc(@Param("account") String account);
    /***
     * @description: 查：医生列表
     * @param:
     * @return: 所有医生数据集合
     * @author shanshanfan
     * @date: 2022/8/29 11:50
     */
    @Select("select * from hrp_doctor")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "name", property = "name"),
            @Result(column = "description", property = "description"),
            @Result(column = "status", property = "status"),
            @Result(column = "account", property = "account"),
            @Result(column = "password", property = "password"),
            @Result(column = "Department_id", property = "department.id"),
            @Result(column = "position_id", property = "position.id"),
            @Result(column = "position_id",property = "position",one = @One(select = "com.woniuxy.hrp.mapper.PositionMapper.getById")),
            @Result(column = "Department_id",property = "department",one = @One(select = "com.woniuxy.hrp.mapper.DepartmentMapper.getById")),
    })
    List<Doctor> selectAll();
    @Select("select * from hrp_doctor")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "name", property = "name"),
            @Result(column = "description", property = "description"),
            @Result(column = "status", property = "status"),
            @Result(column = "account", property = "account"),
            @Result(column = "password", property = "password"),
            @Result(column = "Department_id", property = "department.id"),
            @Result(column = "position_id", property = "position.id"),
    })
    /**
     * @Author  李国杰
     * @Description //suoyoui
     * @Date 10:34 2022/9/6
     * @Param []
     * @return java.util.List<com.woniuxy.hrp.entity.Doctor>
     **/
    List<Doctor> getAll();
    /**
     *@Description 医生功能：查询id医生个人信息
     *@Author TJ
     *@Version 1.0
     **/
    @Select("select * from hrp_doctor where id=#{id}")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "name", property = "name"),
            @Result(column = "description", property = "description"),
            @Result(column = "status", property = "status"),
            @Result(column = "account", property = "account"),
            @Result(column = "password", property = "password"),
            //一对多
            @Result(column = "id", property = "perms", many = @Many(select = "com.woniuxy.hrp.mapper.PermMapper.getByDoctorId", fetchType = FetchType.LAZY)),
            @Result(column = "position_id",property = "position",one = @One(select = "com.woniuxy.hrp.mapper.PositionMapper.getById")),
            @Result(column = "id", property = "perms", many = @Many(select = "com.woniuxy.hrp.mapper.PermMapper.getByDoctorId", fetchType = FetchType.LAZY)),
            @Result(column = "id", property = "position", many = @Many(select = "com.woniuxy.hrp.mapper.PositionMapper.getById", fetchType = FetchType.LAZY)),
            @Result(column = "id", property = "department", many = @Many(select = "com.woniuxy.hrp.mapper.DepartmentMapper.getById", fetchType = FetchType.LAZY))
    })
    Doctor selectByDoctorId(int id);

    /**
     *@Description 医生功能：修改个人密码
     *@Author TJ
     *@Version 1.0
     **/
    @Update("update hrp_doctor set password=#{password} where id=#{id} ")
    void updatePassword(@Param("id")int id,@Param("password")String password);

    /**
     *@Description 医生功能：修改预约状态
     *@Author TJ
     *@Version 1.0
     **/
    @Select("select * from hrp_doctor where id=#{id}")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "id",property = "reservations",many = @Many(select = "com.woniuxy.hrp.mapper.ReservationMapper.findByDoctorId")),
    })
    void findById(int id);

    /**
     *@Description 医生功能：按时间查询预约信息
     *@Author TJ
     *@Version 1.0
     **/
    @Select("select * from hrp_doctor where id=#{id}")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "id",property = "reservations",one = @One(select = "com.woniuxy.hrp.mapper.ReservationMapper.findByIdOrderByTime")),
    })
    Doctor findByIdOrderByTime(int id);
    Doctor seleDPById(@Param("id") int id);

    @Select("select * from hrp_doctor where account=#{account}")
    Doctor getByAccount(String account);

    @Select("select * from hrp_doctor where id=#{id}")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "id",property = "doctorAndTimes",many = @Many(select = "com.woniuxy.hrp.mapper.DoctorAndTimeMapper.selectPatientsbydoctorid")),
    })
    Doctor getPatientByDoctorId(int id);
    /**
     *@Description 根据患者id增加患者药品和数量
     *@Author TJ
     *@Version 1.0
     **/
    @Insert("insert into hrp_doctor_patient_medicine(mrecord_id,doctor_id,patient_id,medicine_id,datetime,num,status) values(#{mrecord_id},#{doctor_id},#{patient_id},#{medicine_id},#{datetime},#{num},#{status})")
    void addPatientMedicine(DoctorPatientMedicine doctorPatientMedicine);
}
